//
//  UIViewController+Show.m
//  OCBase
//
//  Created by 侯伟 on 2019/4/16.
//  Copyright © 2019年 侯伟. All rights reserved.
//

#import "UIViewController+Show.h"
#import "UIViewController+Top.h"

@implementation UIViewController (Show)

/// 显示 view controller（根据当前上下文，自动选择 push 或 present 方式）
+ (void)showViewController:(UIViewController*)controller animated:(BOOL)animated{
    
    UIViewController* topViewController = [UIViewController topViewController];
    
    if([topViewController isKindOfClass:[UINavigationController class]]){
        
        UINavigationController* nav = topViewController.navigationController;
        controller.hidesBottomBarWhenPushed = YES;
        [nav pushViewController:controller animated: animated];
        
    }else if(topViewController.navigationController){
        controller.hidesBottomBarWhenPushed = YES;
        [topViewController.navigationController pushViewController:controller animated: animated];
    }else{
        
        [topViewController presentViewController:controller animated:animated completion: nil];
    }
}

/// 关闭 view controller（根据当前上下文，自动选择 pop 或 dismiss 方式）
+ (void)closeViewControllerAnimated:(BOOL)animated{
    [[UIViewController topViewController] closeViewControllerAnimated:animated];
}

/// 显示 view controller（根据当前上下文，自动选择 push 或 present 方式）
- (void)showViewControllerAnimated:(BOOL)animated{
    [UIViewController showViewController:self animated: animated];
}

/// 关闭 view controller（根据当前上下文，自动选择 pop 或 dismiss 方式）
- (void)closeViewControllerAnimated:(BOOL)animated{

    if(self.navigationController && self.navigationController.viewControllers.count > 1){
        [self.navigationController popViewControllerAnimated:animated];
    }else{
        [self dismissViewControllerAnimated:animated completion: nil];
    }
}

@end
